package com.bangbang.controller;


import com.bangbang.configresult.Result;
import com.bangbang.configresult.ResultEnum;
import com.bangbang.mapper.TbCourseMapper;
import com.bangbang.mapper.TbPeopleCourseMapper;
import com.bangbang.mapper.TbPeopleCourseOrderMapper;
import com.bangbang.pojo.TbCourse;
import com.bangbang.pojo.TbPeopleCourse;
import com.bangbang.pojo.TbPeopleCourseOrder;
import com.bangbang.util.IdWorker;
import com.bangbang.util.ReducePriceUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 砍价课程商品信息表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-08-13
 */
@RestController
@RequestMapping("tbPeopleCourse")
public class TbPeopleCourseController {

    @Autowired
    private TbPeopleCourseMapper tbPeopleCourseMapper;

    @Autowired
    private TbPeopleCourseOrderMapper tbPeopleCourseOrderMapper;

    @Autowired
    private TbCourseMapper tbCourseMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    //    查询普通商品的内容,把路面的信息放到砍价信息里面
    @ApiOperation(value = "普通课程查询方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseId",value = "课程id",required = true,paramType = "path"),
    })
    @GetMapping("/{courseId}")
    public Result selectById(@PathVariable("courseId") Long courseId){
        TbCourse tbCourse = tbCourseMapper.selectById(courseId);
        return Result.buildResult(ResultEnum.SUCCESS).setResult(tbCourse);
    }

    //      新增商品
    @ApiOperation(value = "砍价课程增加方法") //  接口文档方法名
    @ApiImplicitParams({            //  接口文档  解释参数
            @ApiImplicitParam(name = "tbPeopleCourse",value = "砍价课程实体类",required = true,paramType = "body"),
            @ApiImplicitParam(name = "tbCourse",value = "普通课程实体类",required = true,paramType = "body"),
    })
    @PostMapping
    public Result save(@RequestBody TbPeopleCourse tbPeopleCourse,@RequestBody TbCourse tbCourse){
        String couName = tbCourse.getCouName();  // 课程名
        BigDecimal couPrice = tbCourse.getCouPrice(); // 价格
        String couSynopsis = tbCourse.getCouSynopsis(); // 内容
        String couName1 = tbCourse.getCouName();  // 目录名
        String couUrl = tbCourse.getCouUrl();// 图片
        Long couId = tbCourse.getCouId(); // 商品id
        tbPeopleCourse.setPeoCouId(couName)
                .setPeoPrice(couPrice)
                .setPeoContent(couSynopsis)
                .setPeoCourse(couName1)
                .setProPicture(couUrl)
                .setPeoCommoncourseId(couId)
                .setPeoStartTime(new Date());
        BigDecimal peoCouPrice = tbPeopleCourse.getPeoCouPrice();
        String string = peoCouPrice.toString();
        Integer price = Integer.valueOf(string);
        String peoPeople = tbPeopleCourse.getPeoPeople();
        Integer people = Integer.valueOf(peoPeople);
//        将砍的金额   放入redis中
        ReducePriceUtils reducePriceUtils = new ReducePriceUtils();
        List<Double> doubles = reducePriceUtils.splitReducePrice(price,people);
        String s = doubles.toString();
        redisTemplate.boundValueOps("price").set(s);
        tbPeopleCourseMapper.insert(tbPeopleCourse);
        return Result.buildResult(ResultEnum.SUCCESS);
    }

//    订单的查询

    @ApiOperation(value = "砍价课程查询方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "砍价订单号",required = true,paramType = "path"),
    })
    @GetMapping("/{orderId}")
    public Result selectCourseOrderById(@PathVariable("orderId") Long orderId){
        QueryWrapper<TbPeopleCourseOrder> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("pco_id", orderId);
        TbPeopleCourseOrder tbPeopleCourseOrder = tbPeopleCourseOrderMapper.selectOne(orderQueryWrapper);
        return Result.buildResult(ResultEnum.SUCCESS).setResult(tbPeopleCourseOrder);
    }



//    public static void main(String[] args) throws ParseException {
//        DateUtilZb dateUtilZb = new DateUtilZb();
//        DateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置时间格式
//        String time=sdf.format(new Date());
//        String s = dateUtilZb.addDate(time, 2);
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date parse = simpleDateFormat.parse(s);
//        System.out.println(parse);
//    }


}

